package com.nlp.visualization.crawler.pipeline;

import com.google.gson.*;
import com.nlp.visualization.dao.weibo.IWeiboContentMod0;
import com.nlp.visualization.dao.weibo.IWeiboContentMod2;
import com.nlp.visualization.pojo.weibo.*;
import com.nlp.visualization.service.impl.weibo.WeiboContentMod0ServiceImpl;
import com.nlp.visualization.service.impl.weibo.WeiboFollowServiceImpl;
import com.nlp.visualization.service.impl.weibo.WeiboUserinfoServiceImpl;
import com.nlp.visualization.service.weibo_mine.*;
import com.nlp.visualization.utils.SynDatecUtil;
import com.nlp.visualization.utils.SynWeiboUtil;
import com.nlp.visualization.utils.TimeHelper;
import com.nlp.visualization.utils.WeiboUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LXM
 * @Title: WeiboSQLPipeline
 * @Description: 微博数据保存进数据库的服务类
 * @date 2018/3/17上午11:48
 */
@Service
public class WeiboSQLPipeline implements Pipeline {

    Logger logger = LoggerFactory.getLogger(WeiboSQLPipeline.class);


//    @Autowired
//    IHashService hashService;

    @Autowired
    IWeiboUserinfoService weiboUserinfoService;

    @Autowired
    IWeiboContentMod0Service weiboContentMod0Service;

    @Autowired
    IWeiboFollowService weiboFollowService;

    private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");


    @Override
    public void process(ResultItems resultItems, Task task) {

        WeiboList list = (WeiboList) resultItems.get("weibo");
        WeiboUserInfo userInfo = (WeiboUserInfo) resultItems.get("userinfo");
        WeiboUserDetail userDetail = (WeiboUserDetail) resultItems.get("userDetail");
        WeiboUserFollow follow = (WeiboUserFollow) resultItems.get("follow");
        if (list != null) {
            List<WeiboList.DataBean.CardsBean> weiboCardList = list.getData().getCards();
            for (WeiboList.DataBean.CardsBean card : weiboCardList) {

                TWeiboContentMod0 weiboContent = new TWeiboContentMod0();
                weiboContent.setDevices(card.getMblog().getSource());
                String text = StringEscapeUtils.escapeSql(delHTMLTag((card.getMblog().getText())));
                weiboContent.setText(SynWeiboUtil.removeFourCharStr(text));
                weiboContent.setUserId(card.getMblog().getUser().getId() + "");
                weiboContent.setWeiboItemId(card.getMblog().getIdstr());
                String source = StringEscapeUtils.escapeSql(new Gson().toJson(card));
                source = SynWeiboUtil.emojifilter(source);
                weiboContent.setJsonSource(source);
                if (card.getMblog().getRetweeted_status() == null)
                    weiboContent.setIsResend("n");
                else
                    weiboContent.setIsResend("y");
                weiboContent.setRepost(Integer.parseInt(card.getMblog().getReposts_count() + ""));
                weiboContent.setUpvote(Integer.parseInt(card.getMblog().getAttitudes_count() + ""));
                weiboContent.setPublishTime(getPublishTime(card.getMblog().getCreated_at()));
                try {
                    weiboContentMod0Service.saveOrUpdate(weiboContent);
                } catch (Exception e) {
                    logger.error("保存微博失败");
                    e.printStackTrace();
                }
            }

        }
        if (userInfo != null) {
            //普通页面的数据
            TWeiboUserinfo tWeiboUserinfo = new TWeiboUserinfo();
            tWeiboUserinfo.setDescription(SynWeiboUtil.removeFourCharStr(userInfo.getData().getUserInfo().getDescription()));
            tWeiboUserinfo.setFollowCount(userInfo.getData().getUserInfo().getFollow_count());
            tWeiboUserinfo.setFollowerCount(Integer.parseInt(userInfo.getData().getUserInfo().getFollowers_count() + ""));
            tWeiboUserinfo.setGender(userInfo.getData().getUserInfo().getGender());
            tWeiboUserinfo.setHometown("");
            tWeiboUserinfo.setImageUrl(userInfo.getData().getUserInfo().getProfile_image_url());
            tWeiboUserinfo.setName(userInfo.getData().getUserInfo().getScreen_name());
            tWeiboUserinfo.setJob(userInfo.getData().getUserInfo().getVerified_reason());
            tWeiboUserinfo.setJsonSource(SynWeiboUtil.emojifilter(new Gson().toJson(userInfo)));
            tWeiboUserinfo.setUserId(userInfo.getData().getUserInfo().getId() + "");

            try {
                weiboUserinfoService.saveOrUpdate(tWeiboUserinfo);
            } catch (Exception e) {
                logger.error("保存微博用户失败");
                e.printStackTrace();
            }


        } else if (userDetail != null) {
            //详情页面的数据
            Map map = new HashedMap();
            String userid = userDetail.getData().getCardlistInfo().getContainerid().split("_-_INFO")[0].substring(6, 16);
            map.put("user_id", userid);
            logger.info("查询用户：" + userid);
            TWeiboUserinfo tWeiboUserinfo = weiboUserinfoService.findByField(map);
            if (tWeiboUserinfo != null) {
                //清空map，重用
                map.clear();
                List<WeiboUserDetail.DataBean.CardsBean> cards = userDetail.getData().getCards();
                for (WeiboUserDetail.DataBean.CardsBean card : cards) {
                    List<WeiboUserDetail.DataBean.CardsBean.CardGroupBean> group = card.getCard_group();
                    if (group != null)
                        for (int i = 0; i < card.getCard_group().size(); i++) {
                            map.put(group.get(i).getItem_name(), card.getCard_group().get(i).getItem_content());
                        }

                }
                if (map.get("所在地") != null)
                    tWeiboUserinfo.setHometown(map.get("所在地") + "");
                if (map.get("简介") != null)
                    tWeiboUserinfo.setDescription(map.get("简介") + "");
                try {
                    weiboUserinfoService.saveOrUpdate(tWeiboUserinfo);
                } catch (Exception e) {
                    logger.error("保存微博用户失败");
                    e.printStackTrace();
                }
            } else {
                logger.error("该用户不存在：" + userid);
            }
        } else if (follow != null) {
            //遍历粉丝的资料
            for (WeiboUserFollow.DataBean.CardsBean.CardGroupBean groupBean : follow.getData().getCards().get(0).getCard_group()) {
                try {

                    TWeiboFollowPK followPK = new TWeiboFollowPK();
                    TWeiboFollow tWeiboFollow = new TWeiboFollow();
                    String uid = "";
                    String fid = "";

                    uid = follow.getData().getCardlistInfo().getContainerid().split("_-_followers_-_")[1];
                    fid = groupBean.getUser().getId();

                    followPK.setUserId(uid);
                    followPK.setFollowId(fid);
                    tWeiboFollow.setTWeiboFollowPK(followPK);

                    weiboFollowService.saveOrUpdate(tWeiboFollow);
                } catch (Exception e) {
                    logger.error("保存follow关系异常");
                    e.printStackTrace();

                }


            }

        } else {
            return;
        }

    }

    private String getPublishTime(String created_at) {

        String reStr = TimeHelper.getTime() + "";

        if (created_at.indexOf("秒前") != -1) {
            reStr = TimeHelper.getTime() + "";
        } else if (created_at.indexOf("分钟前") != -1) {
            reStr = TimeHelper.getTime() + "";
        } else if (created_at.indexOf("小时前") != -1) {
            reStr = TimeHelper.getTime() + "";
        } else if (created_at.indexOf("昨天") != -1) {
            reStr = TimeHelper.getTime().subtract(new BigInteger(24 * 60 * 60 * 1000 + "")) + "";
        } else if (created_at.indexOf("前天") != -1) {
            reStr = TimeHelper.getTime().subtract(new BigInteger(2 * 24 * 60 * 60 * 1000 + "")) + "";
        } else if (created_at.indexOf("201") != -1) {
            try {
                reStr = (SynDatecUtil.parse(created_at)).getTime() + "";
            } catch (ParseException e) {
                logger.error(reStr + "转换时间戳失败");
                e.printStackTrace();
            }

        } else {
            try {

                String addYear = "2018-" + created_at;
                reStr = (SynDatecUtil.parse(addYear)).getTime() + "";
            } catch (Exception e) {
                logger.error(reStr + "转换时间戳失败");
                e.printStackTrace();
            }

        }
        return reStr;

    }

    /**
     * 过滤html标签
     *
     * @param htmlStr
     * @return
     */
    public static String delHTMLTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); //过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); //过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }


}


